//
//  GasHomeVC.m
//  NtoccDriverNew
//
//  Created by niyongsheng on 2022/10/17.
//  Copyright © 2022 GCY. All rights reserved.
//

#import "GasHomeVC.h"
#import "GasHomeHeaderView.h"
#import "GasHomeModel.h"
#import "GasHomeCell.h"
#import "GasListTabVC.h"
#import "PayListVC.h"
#import "GasSpendListVC.h"
#import "GasStationDriverManager.h"

@interface GasHomeVC ()
<
UITableViewDelegate,
UITableViewDataSource
>
{
    NSInteger _pageNum;
    NSString *_keyword;
}
@property (nonatomic, strong) GasHomeHeaderView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceMArr;
@end

@implementation GasHomeVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([self.typeStr isEqualToString:@"1"]) {
        self.navigationItem.title = @"在线油卡";
        self.headerView.moneyL.text = [NSString stringWithFormat:@"%@元", [self.gasModel.oilCard isNotBlank] ? self.gasModel.oilCard : @"0.00"];
    }else if ([self.typeStr isEqualToString:@"3"]){
        self.navigationItem.title = @"在线气卡";
        self.headerView.moneyL.text = [NSString stringWithFormat:@"%@元", [self.gasModel.gasCard isNotBlank] ? self.gasModel.gasCard : @"0.00"];
    }else if ([self.typeStr isEqualToString:@"2"]){
        self.navigationItem.title = @"在线电卡";
        self.headerView.moneyL.text = [NSString stringWithFormat:@"%@元", [self.gasModel.electricityCard isNotBlank] ? self.gasModel.electricityCard : @"0.00"];

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    @weakify(self)
    self.headerView.sectionBlock = ^(int index) {
        @strongify(self)
        switch (index) {
            case 0: {
                GasListTabVC *vc = [GasListTabVC new];
                vc.typeStr = self.typeStr;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case 1: {
                PayListVC *vc = [PayListVC new];
                vc.typeStr = self.typeStr;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            case 2: {
                GasSpendListVC *vc = [GasSpendListVC new];
                vc.typeStr = self.typeStr;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
                
            default:
                break;
        }
    };
    self.headerView.searchBlock = ^(NSString *searchVal) {
        
    };
    
//    [self.tableView.mj_header beginRefreshing];
}

/// 刷新
/// @param isHeader 是否下拉刷新
- (void)refreshing:(BOOL)isHeader {
    if (isHeader) {
        _pageNum  = 1;
    } else {
        _pageNum ++;
    }
    NSString *urlStr = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],GasHomeOilAreaPrice);
    NSMutableDictionary *params = @{
        @"pageNum" : @(_pageNum),
        @"pageSize" : @9999,
        @"keyword" : _keyword
    }.mutableCopy;
    @weakify(self)
    [HTTPManager getNetworkRequest:urlStr
                     withParameter:params
                            withVC:self
                           netName:@"加油站首页"
                           success:^(id response, NSString *msg) {
        @strongify(self)
        if (isHeader) {
            self.dataSourceMArr = [GasHomeModel mj_objectArrayWithKeyValuesArray:response];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            
        } else {
            NSArray *dictArr = response;
            if (dictArr.count > 0) {
                [self.dataSourceMArr addObjectsFromArray:[GasHomeModel mj_objectArrayWithKeyValuesArray:response]];
                [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
                
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    } failure:^(NSInteger code, NSString *msg) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - TableView 代理方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    GasHomeModel *model = self.dataSourceMArr[section];
    return [self headerSectionView:model.oilsType == 0 ? @"柴油" : @"汽油"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;//35;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSourceMArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellReuseIdentifier = @"GasHomeCell";
    GasHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSourceMArr[indexPath.section];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - MenuBarHeight - NavHeight) style:UITableViewStylePlain];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([GasHomeCell class]) bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:NSStringFromClass([GasHomeCell class])];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        @weakify(self)
//        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            @strongify(self)
//            [self refreshing:YES];
//        }];
//        header.lastUpdatedTimeLabel.hidden = YES;
//        MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
//            @strongify(self)
//            [self refreshing:NO];
//        }];
        
//        _tableView.mj_header = header;
//        _tableView.mj_footer = footer;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (GasHomeHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[GasHomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 250 - 50)];
    }
    return _headerView;
}

- (UIView *)headerSectionView:(NSString *)title {
    CGFloat h = 35;
    UIView *headerSectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, h)];
    headerSectionView.backgroundColor = UIColor.whiteColor;
    
    UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(15, 7, 4, h - 14)];
    tView.backgroundColor = Theme_ColorValue;
    [headerSectionView addSubview:tView];
    
    UILabel *tL = [[UILabel alloc] initWithFrame:CGRectMake(25, 5, 150, h - 10)];
    tL.adjustsFontSizeToFitWidth = YES;
    tL.text = [@"油品类型：" stringByAppendingString:title];
    [headerSectionView addSubview:tL];
    
    return headerSectionView;
}

@end
